
<?php
class Admin_Model_Comment extends Zend_Db_Table{
	protected $_name = 'comment';
	protected $_primary = 'comment_id';
	/* Link tham khao : http://framework.zend.com/manual/en/zend.db.table.html */
	////////////
	/* select*/
	//////////
	public function getAllComment()//
	{//co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/comment/route/getAllComment");
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
				
		return $data;
	}
	/*
// ham lay thong tin comment len
	public function getCommentbyID($thing_id,$page,$offset)
	{
		//lay thong tin comment theo thing_id
		$select = $this->select();
		$select->from('comment');
		$select->setIntegrityCheck(false);
		$select->where('thing_id = ?', $thing_id);
		$select->order('created_date desc');
        $select->limit($offset,($page-1)*$offset);
		$arr = $this->fetchAll($select);
		$i=0;
		// duyet tung dong
		foreach ($arr as $ar)
		{
			if($ar['user_id']!=null)
			{
				// lay thong tin user
				$select=$this->select();
				$select->from("users");
				$select->setIntegrityCheck(false);
				$select->where('user_id = ?', $ar['user_id']);
				$user=$this->fetchRow($select);
					
				if($user->yahoo==null)
					$yahoo = "";
				else 
					$yahoo = $user->yahoo;
					
				if($user->skype==null)
					$skype= "";
				else 
					$skype = $user->skype;
					
				// add du lieu vao2 mang
				$comment[$i]=array(
				"comment_id" =>$ar['comment_id'],
				"title" =>$ar['title'],
				"description" =>$ar['description'],
				"created_date" =>$ar['created_date'],
				"updated_date" =>$ar['updated_date'],
				"user_id" =>$user->user_id,
				"user_link" =>"",
				"user_image" =>$user->avatar,
				"user_name" =>$user->user_name,
				"yahoo" =>$yahoo,
				"skype" =>$skype,
				);
				$i++;
			}
		}
		return $comment;
	}
	public function getReply($thing_id)
	{
		
		$select = $this->select();
		$select->from('comment');
		$select->setIntegrityCheck(false);
		$select->where('thing_id = ?', $thing_id);
		$arr = $this->fetchAll($select);
		$i=0;
		
		$comment = array();
		foreach ($arr as $ar)
		{
			if($ar['user_id']!=null)
			{
				$tbUsers = new User_Model_Users();
				$users = $tbUsers->selectUser($ar['user_id']);
				$user = $users->current();
					
				$comment[$i]=array(
				"description" =>$ar['description'],
				"updated_date" =>$ar['updated_date'],
				"user_id" =>$user["user_id"],
				"user_name" => $user["user_name"],
				);
				$i++;
			}
		}
		return $comment;
	}
	// ham dem xem co bao nhieu comment
	public function countCommentbyID($ID)
    {   
   		$select = $this->select();
		$select->from('comment');
		$select->setIntegrityCheck(false);
		$select->where('thing_id = ?', $ID);
		$arr = $this->fetchAll($select);
		return count($arr);

    }
	
	////////////
	/* insert
	//////////
	// ham them mot comment
	public function insertComment($array)
	{
		$adapter = $this->getDefaultAdapter();
		try {
			$adapter->beginTransaction();
			$id = $adapter->insert('comment', $array);
			$adapter->commit();
			
		}
		catch (Zend_Db_Table_Exception $e) {
			$adapter->rollBack();
			return -1;
		}
		return $id;
	
	}
	
	////////////
	/* delete
	//////////
	public function deleteCommentByID($id)
	{
		$adapter = $this->getDefaultAdapter();
		try {
			$adapter->beginTransaction();
			$where = $adapter->quoteInto('comment_id = ?', $id);
			$adapter->delete('comment',$where);
			$adapter->commit();
		}
		catch (Zend_Db_Table_Exception $e) {
			$adapter->rollBack();
			return -1;
		}
		return 1;
	}*/
}
?>
